/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import self.awt.IPlotter;
import self.awt.event.IEditingListener;
import self.gee.IDiagramCanvas;
import self.gee.editor.IDiagramInspector;
import self.gee.editor.IDiagramToolSet;
import self.gee.editor.IPropertySheet;
import self.swing.PlottingPalette;
import self.swing.PlottingPane;

public class BasicEditingEnvironment extends JPanel {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  BorderLayout borderLayout1 = new BorderLayout();
  PlottingPalette commands = new PlottingPalette();
  JPanel content = new JPanel();
  JPanel viewer = new JPanel();
  PlottingPane plotArea = new PlottingPane();
  BorderLayout borderLayout2 = new BorderLayout();
  BorderLayout borderLayout3 = new BorderLayout();

  JSplitPane plotAreaSplitter = new JSplitPane();
  JPanel inspectorArea = new JPanel();
  JButton acceptChg = new JButton();
  JPanel inspectorCmds = new JPanel();
  BorderLayout borderLayout4 = new BorderLayout();
  JButton revertChg = new JButton();
//  JLabel inspectorLabel = new JLabel();
  JPanel inspectorAccRev = new JPanel();
  BorderLayout borderLayout5 = new BorderLayout();
  Font smallButtonLabels = new java.awt.Font("Arial", 0, 10);
  BorderLayout borderLayout6 = new BorderLayout();
  Border border1;
  JCheckBox autoCommit = new JCheckBox(); 

//  JScrollPane inspectorHost = new JScrollPane();
  JPanel inspectorHost = new JPanel();
  
  public IDiagramToolSet     toolset = new ToolSet();
  public IDiagramCanvas      canvas = new Canvas();
  public IDiagramInspector   inspector = new Inspector();

//  private int inspectorHostContentCount;
  public void setAutoCommitHandler( ActionListener lsn ) {
    autoCommit.addActionListener( lsn );
  }
  
  public boolean getAutoCommitStatus() {
    return autoCommit.isSelected();
  }
  
  public void setAutoCommitStatus( boolean v ) {
    autoCommit.setSelected( v );
  }

  public BasicEditingEnvironment() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
 
  private void jbInit() throws Exception {
//    Container content = this.getContentPane();
    final Container localContent = this;
    border1 = BorderFactory.createEmptyBorder();
    localContent.setLayout( borderLayout1 );
    localContent.setLayout(borderLayout2);
    viewer.setLayout(borderLayout3);
    plotAreaSplitter.setContinuousLayout(true);
    plotAreaSplitter.setDividerLocation(250);
    plotAreaSplitter.setLeftComponent(inspectorArea);
    plotAreaSplitter.setOneTouchExpandable(true);
    plotAreaSplitter.setRightComponent(plotArea);
    acceptChg.setFont( smallButtonLabels );
    acceptChg.setPreferredSize(new Dimension(70, 16));
    acceptChg.setText("Accept");
    acceptChg.setMnemonic( 'A' );
    acceptChg.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        acceptChg_actionPerformed(e);
      }
    });
    inspectorArea.setBorder(border1);
    inspectorArea.setPreferredSize(new Dimension(250, 60));
    inspectorArea.setLayout(borderLayout4);
    revertChg.setFont(new java.awt.Font("Arial", 0, 10) );
    revertChg.setPreferredSize(new Dimension(70, 16));
    revertChg.setText("Revert");
    revertChg.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        revertChg_actionPerformed(e);
      }
    });
//    inspectorLabel.setForeground(SystemColor.window);
//    inspectorLabel.setPreferredSize(new Dimension(30, 22));
//    inspectorLabel.setText(" Inspector ");
    inspectorCmds.setBackground(Color.gray);
    inspectorCmds.setPreferredSize(new Dimension(220, 22));
    inspectorCmds.setLayout(borderLayout5);
    inspectorAccRev.setBackground(Color.gray);
    inspectorAccRev.setPreferredSize(new Dimension(155, 22));
    autoCommit.setForeground(SystemColor.window);
    autoCommit.setBackground(Color.gray);
    autoCommit.setText(" auto accept");
    autoCommit.setSelected( true );
    autoCommit.setHorizontalTextPosition(SwingConstants.LEADING);
//    autoCommit.addActionListener(new java.awt.event.ActionListener() {
//      public void actionPerformed(ActionEvent e) {
//        autoCommit_actionPerformed(e);
//      }
//    });
    localContent.add(commands, BorderLayout.NORTH);
    localContent.add(viewer, BorderLayout.CENTER);
    
    viewer.add(plotAreaSplitter, BorderLayout.CENTER);
    
//    viewer.add(plotArea, BorderLayout.CENTER);
//    viewer.add(inspectorArea, BorderLayout.WEST);

    inspectorArea.add(inspectorCmds, BorderLayout.NORTH);
    inspectorCmds.add(autoCommit, BorderLayout.WEST);
    inspectorCmds.add(inspectorAccRev, BorderLayout.EAST);
//    inspectorAccRev.add(autoCommit, null);
    inspectorAccRev.add(acceptChg, null);
    inspectorAccRev.add(revertChg, null);
    inspectorHost.setLayout( borderLayout6 );
    inspectorArea.add(inspectorHost, BorderLayout.CENTER);
    viewer.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
//    inspectorHostContentCount = inspectorHost.getComponentCount();
  }
  
  void acceptChg_actionPerformed(ActionEvent e) {
    IPropertySheet sheet =  inspector.getSheet();
    if ( sheet != null )
      sheet.accept();
  }

  void revertChg_actionPerformed(ActionEvent e) {
    IPropertySheet sheet =  inspector.getSheet();
    if ( sheet != null )
      sheet.revert();
  }

  class Canvas implements IDiagramCanvas {
    public void setPlotter( IPlotter agent ) {
      plotArea.setPlotter( agent );
    }
    
    public void addEditingListener( IEditingListener l ) {
      plotArea.addMouseListener( l );
      plotArea.addMouseMotionListener( l );
      plotArea.addKeyListener( l );
    }
    
    public void removeEditingListener( IEditingListener l ) {
      plotArea.removeMouseListener( l );
      plotArea.removeMouseMotionListener( l );
      plotArea.removeKeyListener( l );
    }
    
    public void redraw() {
      plotArea.repaint();
    }
  }
  
  class ToolSet implements IDiagramToolSet {
    public void selectDefault() {
      commands.selectDefault();
    }
    
    public void install(List installToolset) {
      commands.clear();
      final Iterator itr = installToolset.iterator();
      while (itr.hasNext()) {
        final Action act = (Action) itr.next();
        commands.addAction(act);
      }
      commands.setVisible(false);
      commands.setVisible(true);
      commands.selectDefault();
    }
  }
  
  class Inspector implements IDiagramInspector {
    private IPropertySheet sheet;
    public void install( IPropertySheet propertySheet ) {
      sheet = propertySheet;
      Component sheetAsComponent = sheet.toComponent();
//      System.out.println("inspectorHostContentCount=" + inspectorHostContentCount );
//      if ( inspectorHostContentCount < inspectorHost.getComponentCount() ) {
//        Component toRem = inspectorHost.getComponent( inspectorHostContentCount );
//        System.out.println("removing=" + toRem );
//        inspectorHost.remove( inspectorHostContentCount );
//      }
      inspectorHost.removeAll();
      inspectorHost.add( sheetAsComponent, BorderLayout.CENTER );
      
      SwingUtilities.updateComponentTreeUI( BasicEditingEnvironment.this );
      
      BasicEditingEnvironment.this.revalidate();
      BasicEditingEnvironment.this.repaint();
    }
    
    public void reset() {
//      inspectorHost.getViewport().setView( null );
//      inspectorHost.remove( inspectorHostContentCount );
      inspectorHost.removeAll();
      BasicEditingEnvironment.this.revalidate();
      BasicEditingEnvironment.this.repaint();
      sheet = null;
    }
    
    public IPropertySheet getSheet() {
      return sheet;
    }
  }
}
